﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Elmah;
using MvcSiteMapProvider.Web;
using Beeldbankmvc.Helpers;

namespace Beeldbankmvc
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "DisplayImage",                                 // Route name
                "Custom/BB/DisplayImage.aspx",                  // URL with parameters
                new { controller = "Home", action = "Lost" }    // Parameter defaults
            );

            routes.MapRoute(
                "Create",                                                                                   // Route name
                "MediaObject/Create/{mediaTypeId}",                                                         // URL with parameters
                new { controller = "MediaObject", action = "Create", mediaTypeId = UrlParameter.Optional }  // Parameter defaults
            );

            routes.MapRoute(
                "Index",                                                                            // Route name
                "KeyWord/list/{letter}",                                                            // URL with parameters
                new { controller = "KeyWord", action = "Index", letter = UrlParameter.Optional }    // Parameter defaults
            );

            routes.MapRoute(
                "MediaServiceGetImage",                                                                     // Route name
                "MediaService/{pictureName}",                                                               // URL with parameters
                new { controller = "MediaService", action = "Index", pictureName = UrlParameter.Optional }  // Parameter defaults
            );

            routes.MapRoute(
                "Default",                                                                  // Route name
                "{controller}/{action}/{id}",                                               // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }   // Parameter defaults
            );

            routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
        }

        // Keep checking if user is logged-in or not
        // Depending upon the result caching is useed based upon the user that is currently logged in,
        // or the default anonymous users
        public override string GetVaryByCustomString(HttpContext context, string arg)
        {
            if (arg == "IsLoggedIn")
            {
                if (context.Request.IsAuthenticated)
                    return "auth_" + context.User.Identity.Name;
                else
                    return "anon";
            }
            else
                return base.GetVaryByCustomString(context, arg);
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            XmlSiteMapController.RegisterRoutes(RouteTable.Routes); // <-- register sitemap.xml, add this line of code

            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_BeginRequest()
        {
            if (HttpContext.Current.Request.Url.LocalPath.ToLower().Equals("/a") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("custom/") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("bestellijst") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("mediazoeken") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("foto39senvideo39szoeken") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("videobekijken") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("beeldarchief/") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("apps") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("imagegrabber") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("beeldarchief.aspx") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("desktopmodules") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("disclaimer") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("gemonteerdevideos") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("tabid") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("videobekijken") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("displaydetail?") ||
                HttpContext.Current.Request.Url.ToString().ToLower().Contains("displaythumb?")) 
            {
                HttpContext.Current.Response.Status = "301 Moved Permanently";

                HttpContext.Current.Response.AddHeader("Location", "https://beeldbank.rws.nl");
            }
        }

        void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
        {
            // This should dismiss all known "xxx?mediaobjectid=" errors for msnbot
            // So far it doesn't work though
            if (Request.UserAgent != null && Request.UserAgent.ToString().Contains("msnbot"))
            {
                if (Request.Url.ToString().ToLower().Contains("?mediaobjectid=")) e.Dismiss();
            }
        }
    }
}